package com.cobo.fitness.web.fitness.services.impl;

import com.cobo.fitness.core.constant.FitnessIndexConst;
import com.cobo.fitness.core.meillisearch.SearchApi;
import com.cobo.fitness.web.fitness.services.ExerciseService;
import com.cobo.fitness.web.fitness.services.SearchService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class SearchServiceImpl implements SearchService {

    @Autowired
    private ExerciseService exerciseService;

    @Autowired
    private SearchApi searchApi;

    @Override
    public void sync() {
//        long totalNum = exerciseService.count();
//        int limit = 100;
//        long partions = totalNum / limit;
//
//        long last = totalNum % limit;
//
//        for (int i = 1; i <= partions; i++) {
//            int offset = limit * (i-1);
//          List<Exercise> exercises= exerciseService.lambdaQuery()
//                  .select(Exercise::getId,Exercise::getName,Exercise::getTarget,Exercise::getName,Exercise::getBodyPart,Exercise::getEquipment,Exercise::getGifUrl)
//                    .last("limit " + offset + "," + limit)
//                    .list();
//            List<ExerciseResVo> items = exercises.stream().map(exerciseService::transformToRes).collect(Collectors.toList());
//            searchApi.addDocument(FitnessIndexConst.FITNESS, items);
//        }
//        int offset = (int) (limit * (partions-1));
//        List<Exercise> exercises = exerciseService.lambdaQuery()
//                .select(Exercise::getId,Exercise::getName,Exercise::getTarget,Exercise::getName,Exercise::getBodyPart,Exercise::getEquipment,Exercise::getGifUrl)
//                .last("limit " + offset + "," + limit)
//                .list();
//        List<ExerciseResVo> items = exercises.stream().map(exerciseService::transformToRes).collect(Collectors.toList());
//        searchApi.addDocument(FitnessIndexConst.FITNESS, items);
        searchApi.updateFilter(FitnessIndexConst.FITNESS, new String[]{"bodyPart"});
    }
}
